package com.ssh.service;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;

import javax.inject.Inject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.ssh.dao.IUserDao;
import com.ssh.exception.UserException;
import com.ssh.model.Pager;
import com.ssh.model.User;

@Service("userService")
public class UserService implements IUserService {
	
	private IUserDao userDao;
	
	public IUserDao getUserDao() {
		return userDao;
	}

	@Inject
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public void addUser(User user) {
		try {
			User u = userDao.getUser(user.getName());
			if(u!=null){
				throw new UserException(user.getName() + " is already existed.");
			}else{
				userDao.addUser(user);
			}
		} catch (Exception e) {
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw));
			Logger.getLogger("service").debug("service:adding a user..."+sw.toString());
		}
	}

	@Override
	public void updateUser(User user) {
		try {
			User u = userDao.getUser(user.getId(), user.getName());
			if(u!=null){
				throw new UserException(user.getName() + " is already existed.");
			}else{
				userDao.updateUser(user);
				Logger.getLogger("service").debug("service:updating a user...");
			}
		} catch (Exception e) {
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw));
			Logger.getLogger("service").debug("service:updating a user..."+sw.toString());
		}
	}

	@Override
	public void deleteUser(int id) {
		try {
			userDao.deleteUser(id);
			Logger.getLogger("service").debug("service:deleting a user...");
		} catch (Exception e) {
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw));
			Logger.getLogger("service").debug("service:deleting a user..."+sw.toString());
		}
	}

	@Override
	public List<User> getUsers() {
		List<User> users = null;
		try {
			users = userDao.getUsers();
		} catch (Exception e) {
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw));
			Logger.getLogger("service").debug("service:getting users..."+sw.toString());
		}
		return users;
	}

	@Override
	public User getUser(int id) {
		User user = null;
		try {
			user = userDao.getUser(id);
		} catch (Exception e) {
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw));
			Logger.getLogger("service").debug("service:getting a user by id..."+sw.toString());
		}
		return user;
	}
	

	@Override
	public Pager<User> findUsers() {
		Pager<User> users = null;
		try {
			users = userDao.findUsers();
		} catch (Exception e) {
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw));
			Logger.getLogger("service").debug("service:getting pagers..."+sw.toString());
		}
		return users;
	}
	
	@Override
	public User login(String name, String password) {
		User u = null;
		try {
			u = userDao.getUser(name);
			if(u==null) {
				throw new UserException("no user's name like : "+name);
			}else if(!u.getPassword().equals(password)){
				throw new UserException("password is not correct");
			}
		} catch (Exception e) {
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw));
			Logger.getLogger("service").debug("service:logining..."+sw.toString());
		}
		return u;
	}
}
